package com.gzwanchuang.lszg.module.shop;

import android.graphics.Color;
import android.os.Bundle;
import android.text.method.ScrollingMovementMethod;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;

import com.gzwanchuang.lszg.R;
import com.gzwanchuang.lszg.adapter.ShopBean;
import com.gzwanchuang.lszg.adapter.ShopLinearAdapter;
import com.gzwanchuang.lszg.base.BaseActivity;
import com.gzwanchuang.lszg.net.Apis;
import com.gzwanchuang.lszg.net.NetworkManager;
import com.gzwanchuang.lszg.net.WebApis;
import com.gzwanchuang.lszg.net.interfaces.OnRequestCallBack;
import com.gzwanchuang.lszg.proto.Mystock;
import com.gzwanchuang.lszg.utils.StatusBarUtil;
import com.gzwanchuang.lszg.view.pinnedheader.PinnedHeaderItemDecoration;
import com.gzwanchuang.lszg.view.pinnedheader.PinnedHeaderRecyclerView;
import com.gzwanchuang.lszg.view.pinnedheader.SideBar;
import com.gzwanchuang.lszg.web.ui.WebViewActivity;
import com.google.protobuf.InvalidProtocolBufferException;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class ShopNextActivity extends BaseActivity implements ShopLinearAdapter.OnItemClickListener {

    @BindView(R.id.tv_message)
    TextView tvMessage;
    @BindView(R.id.back_iv)
    ImageView backIv;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.recycler_linear)
    PinnedHeaderRecyclerView recyclerLinear;
    @BindView(R.id.sidebar)
    SideBar sidebar;
    @BindView(R.id.tv_ti_shi)
    TextView tvTiShi;


    private List<ShopBean> dataList = new ArrayList<>();
    private List<String> sortList = new ArrayList<>();
    private LinearLayoutManager layoutManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_partner);
        ButterKnife.bind(this);
        StatusBarUtil.setStatusBarLightMode(this, Color.parseColor("#FFFFFF"), true);
        tvMessage.setMovementMethod(new ScrollingMovementMethod());
        tvTitle.setText(getIntent().getStringExtra("title") + " 商户");
        backIv.setOnClickListener(v -> finish());
        initView();
        initData();
    }

    private void initView() {
        sidebar.setTextView(tvTiShi);
        layoutManager = new LinearLayoutManager(this);
        recyclerLinear.setLayoutManager(layoutManager);
        sidebar.setOnTouchingLetterChangedListener(new SideBar.OnTouchingLetterChangedListener() {
            @Override
            public void onTouchingLetterChanged(String s) {
                for (int i = 0; i < dataList.size(); i++) {
                    if (dataList.get(i).equals(s)) {
                        layoutManager.scrollToPositionWithOffset(i, 0);
                        break;
                    }
                }
            }
        });

    }

    private void initData() {
        Mystock.merchant_list.Builder builder = Mystock.merchant_list.newBuilder();
        builder.setGoodsId(getIntent().getType());
        NetworkManager.INSTANCE.post(Apis.get_merchant_list, builder.build().toByteArray(), new OnRequestCallBack() {
            @Override
            public void onOk(byte[] bytes) throws InvalidProtocolBufferException {
                Mystock.merchant_list result = Mystock.merchant_list.parseFrom(bytes);
                Log.e("result",result.toString());
                List<Mystock.list_basis> list = result.getListDesList();
                List<Mystock.list_part> list_parts = result.getListPartList();

                StringBuilder des = new StringBuilder();
                for (int i = 0; i < list.size() - 1; i++) {
                    des.append(list.get(i).getName()).append("\n");
                }
                tvMessage.setText(des.toString());
                for (int i = 0; i < list_parts.size(); i++) {
                    ShopBean titleBean = new ShopBean();
                    titleBean.setTitle(list_parts.get(i).getFirstCharter());
                    titleBean.setType(1);
                    dataList.add(titleBean);
                    sortList.add(list_parts.get(i).getFirstCharter());
                    for (int j = 0; j < list_parts.get(i).getLisBasisList().size(); j++) {
                        ShopBean contentBean = new ShopBean();
                        contentBean.setContent(list_parts.get(i).getLisBasis(j));
                        contentBean.setType(0);
                        dataList.add(contentBean);
                    }
                }
                ShopLinearAdapter adapter = new ShopLinearAdapter(dataList, ShopNextActivity.this);
                recyclerLinear.setAdapter(adapter);
                recyclerLinear.addItemDecoration(new PinnedHeaderItemDecoration());
                sidebar.setList(sortList);
            }

            @Override
            public void onError(int code, String errorMessage) {
                showToast(errorMessage);
            }
        });
    }

    @Override
    public void onClick(View v, int position) {
        WebViewActivity.startActivity(mContext, WebApis.merchant_detail + "?sn_code="+dataList.get(position).getContent().getMobile());


    }
}
